{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "213800fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "08e94150",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1.0\n",
      "1    3.0\n",
      "2    4.0\n",
      "3    5.0\n",
      "4    NaN\n",
      "5    6.0\n",
      "6    7.0\n",
      "dtype: float64\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "# 用python列表创建一个series\n",
    "s = pd.Series([1,3,4,5,np.nan, 6,7])\n",
    "print(s)\n",
    "print(type(s))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9edd33f6",
   "metadata": {},
   "source": [
    "# 2.用列表创建一个DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6d3ec709",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0\n",
      "0   Game\n",
      "1  range\n",
      "2   area\n"
     ]
    }
   ],
   "source": [
    "l = ['Game', 'range','area']\n",
    "df = pd.DataFrame(l)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "840551a7",
   "metadata": {},
   "source": [
    "# 3.用series字典对象生成DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "91d407fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D     E    F\n",
      "0  1 2013-01-02  1.0  3  test  foo\n",
      "1  1 2013-01-02  1.0  3   NaN  foo\n",
      "2  1 2013-01-02  1.0  3  test  foo\n",
      "3  1 2013-01-02  1.0  3   NaN  foo\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame({'A':1,\n",
    "                  'B':pd.Timestamp('20130102'),\n",
    "                  'C':pd.Series(1,index=list(range(4)),dtype='float32'),\n",
    "                  'D':np.array([3]*4, dtype='i1'),\n",
    "                  'E':pd.Categorical(['test','train','test','train'],['test'],ordered=True),\n",
    "                  'F':'foo'})\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c05fae8",
   "metadata": {},
   "source": [
    "# 4.在pandas中创建一个空的DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "115cfec3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: []\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "345b12e7",
   "metadata": {},
   "source": [
    "# 5.查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "84978c07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df:                    A         B         C         D\n",
      "2023-01-01 -1.566628 -0.893076  1.647789  1.423510\n",
      "2023-01-02  0.253095 -0.643432  1.363353  0.651004\n",
      "2023-01-03 -0.105738 -1.324003 -2.027889  0.501216\n",
      "2023-01-04  0.043217 -0.363688 -0.932975 -0.479528\n",
      "2023-01-05 -1.265374 -0.858054  0.575805 -0.173577\n",
      "2023-01-06 -0.363435  0.361244 -1.637252 -0.431182\n",
      "头部数据：                    A         B         C         D\n",
      "2023-01-01 -1.566628 -0.893076  1.647789  1.423510\n",
      "2023-01-02  0.253095 -0.643432  1.363353  0.651004\n",
      "2023-01-03 -0.105738 -1.324003 -2.027889  0.501216\n",
      "2023-01-04  0.043217 -0.363688 -0.932975 -0.479528\n",
      "2023-01-05 -1.265374 -0.858054  0.575805 -0.173577\n",
      "尾部数据：                    A         B         C         D\n",
      "2023-01-02  0.253095 -0.643432  1.363353  0.651004\n",
      "2023-01-03 -0.105738 -1.324003 -2.027889  0.501216\n",
      "2023-01-04  0.043217 -0.363688 -0.932975 -0.479528\n",
      "2023-01-05 -1.265374 -0.858054  0.575805 -0.173577\n",
      "2023-01-06 -0.363435  0.361244 -1.637252 -0.431182\n",
      "数据统计摘要：               A         B         C         D\n",
      "count  6.000000  6.000000  6.000000  6.000000\n",
      "mean  -0.500811 -0.620168 -0.168528  0.248574\n",
      "std    0.742943  0.575490  1.574703  0.745207\n",
      "min   -1.566628 -1.324003 -2.027889 -0.479528\n",
      "25%   -1.039890 -0.884321 -1.461183 -0.366781\n",
      "50%   -0.234586 -0.750743 -0.178585  0.163820\n",
      "75%    0.005978 -0.433624  1.166466  0.613557\n",
      "max    0.253095  0.361244  1.647789  1.423510\n",
      "索引： DatetimeIndex(['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04',\n",
      "               '2023-01-05', '2023-01-06'],\n",
      "              dtype='datetime64[ns]', freq='D')\n",
      "列名： Index(['A', 'B', 'C', 'D'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# 已知数据如下\n",
    "dates = pd.date_range('20230101',periods=6)\n",
    "df=pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))\n",
    "# 输出\n",
    "print('df:', df)\n",
    "# 查看头部数据\n",
    "print('头部数据：', df.head())\n",
    "# 查看尾部数据\n",
    "print('尾部数据：', df.tail())\n",
    "# 查看数据统计摘要\n",
    "print('数据统计摘要：',df.describe())\n",
    "# 查看索引\n",
    "print('索引：', df.index)\n",
    "# 查看列名\n",
    "print('列名：', df.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e7836d",
   "metadata": {},
   "source": [
    "# 6.索引相关操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5d7f77a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            A     X         Y       C           D\n",
      "0  2023-01-03   0.0  0.177416  Medium  116.971770\n",
      "1  2023-01-04   1.0  0.354887     Low   91.444374\n",
      "2  2023-01-05   2.0  0.107699     Low   99.450812\n",
      "3  2023-01-06   3.0  0.895143  Medium  109.498996\n",
      "4  2023-01-07   4.0  0.386453    High  101.283136\n",
      "5  2023-01-08   5.0  0.071290  Medium   97.512792\n",
      "6  2023-01-09   6.0  0.627941     Low   96.543488\n",
      "7  2023-01-10   7.0  0.620014     Low   97.124223\n",
      "8  2023-01-11   8.0  0.405334     Low  106.552835\n",
      "9  2023-01-12   9.0  0.021971    High   86.626614\n",
      "10 2023-01-13  10.0  0.634626     Low   96.438307\n",
      "11 2023-01-14  11.0  0.415041     Low   91.609125\n",
      "12 2023-01-15  12.0  0.706441  Medium  102.209842\n",
      "13 2023-01-16  13.0  0.830653    High   88.739079\n",
      "14 2023-01-17  14.0  0.649945     Low   88.738130\n",
      "15 2023-01-18  15.0  0.466172  Medium  111.325589\n",
      "16 2023-01-19  16.0  0.988194    High   86.404515\n",
      "17 2023-01-20  17.0  0.300955     Low  103.212379\n",
      "18 2023-01-21  18.0  0.746088    High   97.052551\n",
      "19 2023-01-22  19.0  0.769994    High   83.118912\n",
      "           A       C   B\n",
      "0 2023-01-03  Medium NaN\n",
      "2 2023-01-05     Low NaN\n",
      "5 2023-01-08  Medium NaN\n"
     ]
    }
   ],
   "source": [
    "N = 20\n",
    "df=pd.DataFrame({\n",
    "    'A':pd.date_range(start='20230103',periods=N,freq='D'),\n",
    "    'X':np.linspace(0, stop=N-1,num=N),\n",
    "    'Y':np.random.rand(N),\n",
    "    'C':np.random.choice(['Low','Medium','High'], N).tolist(),\n",
    "    'D':np.random.normal(100,10,size=N).tolist()\n",
    "})\n",
    "df_reindexed = df.reindex(index=[0,2,5],columns=['A','C','B'])\n",
    "print(df)\n",
    "print(df_reindexed)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1140a5a8",
   "metadata": {},
   "source": [
    "# 6.1重置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7fbb6c0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Coca Cola    10\n",
      "Sprite       25\n",
      "Coke          3\n",
      "Fanta        11\n",
      "Dew          24\n",
      "ThumbsUp      6\n",
      "dtype: int64\n",
      "       index   0\n",
      "0  Coca Cola  10\n",
      "1     Sprite  25\n",
      "2       Coke   3\n",
      "3      Fanta  11\n",
      "4        Dew  24\n",
      "5   ThumbsUp   6\n"
     ]
    }
   ],
   "source": [
    "\n",
    "sr = pd.Series([10,25,3,11,24,6])\n",
    "# 创建索引\n",
    "index_=['Coca Cola', 'Sprite','Coke','Fanta','Dew',\n",
    "       'ThumbsUp']\n",
    "# 设置索引\n",
    "sr.index=index_\n",
    "print(sr)\n",
    "# 重置索引\n",
    "result = sr.reset_index()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc012bc4",
   "metadata": {},
   "source": [
    "# 7.选择需要的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "3efcc508",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>-1.080636</td>\n",
       "      <td>0.428776</td>\n",
       "      <td>-0.361892</td>\n",
       "      <td>0.827931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>-1.007054</td>\n",
       "      <td>-0.769075</td>\n",
       "      <td>0.032177</td>\n",
       "      <td>0.485353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-1.258868</td>\n",
       "      <td>-0.718085</td>\n",
       "      <td>1.557733</td>\n",
       "      <td>1.009015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>0.445148</td>\n",
       "      <td>0.540572</td>\n",
       "      <td>-0.396411</td>\n",
       "      <td>-1.789355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>-0.908166</td>\n",
       "      <td>-0.612264</td>\n",
       "      <td>0.453686</td>\n",
       "      <td>-2.073644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-06</th>\n",
       "      <td>1.009833</td>\n",
       "      <td>2.168442</td>\n",
       "      <td>0.325244</td>\n",
       "      <td>0.442681</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2023-01-01 -1.080636  0.428776 -0.361892  0.827931\n",
       "2023-01-02 -1.007054 -0.769075  0.032177  0.485353\n",
       "2023-01-03 -1.258868 -0.718085  1.557733  1.009015\n",
       "2023-01-04  0.445148  0.540572 -0.396411 -1.789355\n",
       "2023-01-05 -0.908166 -0.612264  0.453686 -2.073644\n",
       "2023-01-06  1.009833  2.168442  0.325244  0.442681"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates = pd.date_range('20230101',periods=6)\n",
    "df=pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "670f12b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>                    A\n",
      "2023-01-01 -1.080636\n",
      "2023-01-02 -1.007054\n",
      "2023-01-03 -1.258868\n",
      "2023-01-04  0.445148\n",
      "2023-01-05 -0.908166\n",
      "2023-01-06  1.009833\n",
      "<class 'pandas.core.series.Series'> 2023-01-01   -1.080636\n",
      "2023-01-02   -1.007054\n",
      "2023-01-03   -1.258868\n",
      "2023-01-04    0.445148\n",
      "2023-01-05   -0.908166\n",
      "2023-01-06    1.009833\n",
      "Freq: D, Name: A, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "## 7.1 选择单列，产生Series\n",
    "a = df['A']\n",
    "b = df[['A']]\n",
    "print(type(b), b)\n",
    "print(type(a) ,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "36ff3187",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>0.859055</td>\n",
       "      <td>-0.882752</td>\n",
       "      <td>-2.485008</td>\n",
       "      <td>-0.220455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>-0.312689</td>\n",
       "      <td>2.486631</td>\n",
       "      <td>0.333440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>-2.020282</td>\n",
       "      <td>0.076171</td>\n",
       "      <td>-1.017064</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2023-01-01  0.859055 -0.882752 -2.485008 -0.220455\n",
       "2023-01-02  0.535175 -0.312689  2.486631  0.333440\n",
       "2023-01-03 -0.555540 -2.020282  0.076171 -1.017064"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 7.2 用[]切片行：\n",
    "df[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "aa42d96c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>-0.312689</td>\n",
       "      <td>2.486631</td>\n",
       "      <td>0.333440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>-2.020282</td>\n",
       "      <td>0.076171</td>\n",
       "      <td>-1.017064</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>1.174328</td>\n",
       "      <td>-1.167818</td>\n",
       "      <td>0.945664</td>\n",
       "      <td>-0.441212</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2023-01-02  0.535175 -0.312689  2.486631  0.333440\n",
       "2023-01-03 -0.555540 -2.020282  0.076171 -1.017064\n",
       "2023-01-04  1.174328 -1.167818  0.945664 -0.441212"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['20230102':'20230104']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87b335d6",
   "metadata": {},
   "source": [
    "## 7.1按标签选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dcc2385e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.859055\n",
       "B   -0.882752\n",
       "C   -2.485008\n",
       "D   -0.220455\n",
       "Name: 2023-01-01 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用标签提取一行数据\n",
    "df.loc[dates[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8cf32bed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>-0.312689</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>-2.020282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>1.174328</td>\n",
       "      <td>-1.167818</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2023-01-02  0.535175 -0.312689\n",
       "2023-01-03 -0.555540 -2.020282\n",
       "2023-01-04  1.174328 -1.167818"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用标签选择多列数据\n",
    "df.loc['20230102':'20230104',['A','B']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b3d17ed0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.535175\n",
       "B   -0.312689\n",
       "Name: 2023-01-02 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回对象降维\n",
    "df.loc['20230102',['A','B']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ddf29c6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5351748952850612"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取标量值\n",
    "df.loc['20230102','A']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b22216ae",
   "metadata": {},
   "source": [
    "## 7.2按位置选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "cf0ced60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1.174328\n",
       "B   -1.167818\n",
       "C    0.945664\n",
       "D   -0.441212\n",
       "Name: 2023-01-04 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用整数位置选择\n",
    "df.iloc[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "72dd0877",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>1.174328</td>\n",
       "      <td>-1.167818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>-0.306017</td>\n",
       "      <td>1.654402</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2023-01-04  1.174328 -1.167818\n",
       "2023-01-05 -0.306017  1.654402"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用整数切片\n",
    "df.iloc[3:5,0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "262e4dc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>2.486631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>0.076171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>-0.306017</td>\n",
       "      <td>-0.105530</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         C\n",
       "2023-01-02  0.535175  2.486631\n",
       "2023-01-03 -0.555540  0.076171\n",
       "2023-01-05 -0.306017 -0.105530"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用整数列表按位置切片\n",
    "df.iloc[[1,2,4],[0,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "e6de3b70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>-0.312689</td>\n",
       "      <td>2.486631</td>\n",
       "      <td>0.333440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>-2.020282</td>\n",
       "      <td>0.076171</td>\n",
       "      <td>-1.017064</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2023-01-02  0.535175 -0.312689  2.486631  0.333440\n",
       "2023-01-03 -0.555540 -2.020282  0.076171 -1.017064"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示整行切片\n",
    "df.iloc[1:3,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "08c71c60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>-0.882752</td>\n",
       "      <td>-2.485008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>-0.312689</td>\n",
       "      <td>2.486631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-2.020282</td>\n",
       "      <td>0.076171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>-1.167818</td>\n",
       "      <td>0.945664</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>1.654402</td>\n",
       "      <td>-0.105530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-06</th>\n",
       "      <td>-0.625535</td>\n",
       "      <td>0.792208</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   B         C\n",
       "2023-01-01 -0.882752 -2.485008\n",
       "2023-01-02 -0.312689  2.486631\n",
       "2023-01-03 -2.020282  0.076171\n",
       "2023-01-04 -1.167818  0.945664\n",
       "2023-01-05  1.654402 -0.105530\n",
       "2023-01-06 -0.625535  0.792208"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示整列\n",
    "df.iloc[:,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "61242f45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3126894133450385"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示标量值\n",
    "df.iloc[1,1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ad8f0e7",
   "metadata": {},
   "source": [
    "## 7.3布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "be6f3d16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>0.859055</td>\n",
       "      <td>-0.882752</td>\n",
       "      <td>-2.485008</td>\n",
       "      <td>-0.220455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>-0.312689</td>\n",
       "      <td>2.486631</td>\n",
       "      <td>0.333440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>1.174328</td>\n",
       "      <td>-1.167818</td>\n",
       "      <td>0.945664</td>\n",
       "      <td>-0.441212</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2023-01-01  0.859055 -0.882752 -2.485008 -0.220455\n",
       "2023-01-02  0.535175 -0.312689  2.486631  0.333440\n",
       "2023-01-04  1.174328 -1.167818  0.945664 -0.441212"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 用单列的值选择数据\n",
    "df[df.A>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "ef164bd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>0.859055</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.486631</td>\n",
       "      <td>0.33344</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.076171</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>1.174328</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.945664</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.654402</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-06</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.792208</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C        D\n",
       "2023-01-01  0.859055       NaN       NaN      NaN\n",
       "2023-01-02  0.535175       NaN  2.486631  0.33344\n",
       "2023-01-03       NaN       NaN  0.076171      NaN\n",
       "2023-01-04  1.174328       NaN  0.945664      NaN\n",
       "2023-01-05       NaN  1.654402       NaN      NaN\n",
       "2023-01-06       NaN       NaN  0.792208      NaN"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选择DataFrame中满足的值\n",
    "df[df>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a281b6a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>-2.020282</td>\n",
       "      <td>0.076171</td>\n",
       "      <td>-1.017064</td>\n",
       "      <td>two</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>-0.306017</td>\n",
       "      <td>1.654402</td>\n",
       "      <td>-0.105530</td>\n",
       "      <td>-0.907193</td>\n",
       "      <td>four</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D     E\n",
       "2023-01-03 -0.555540 -2.020282  0.076171 -1.017064   two\n",
       "2023-01-05 -0.306017  1.654402 -0.105530 -0.907193  four"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用isin()筛选\n",
    "df2 = df.copy()\n",
    "df2['E'] = ['one', 'one', 'two', 'three', 'four', 'three']\n",
    "df2[df2['E'].isin(['two','four'])]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5856c608",
   "metadata": {},
   "source": [
    "## 7.4 赋值\n",
    "赋值的方式和上面选择值的方式差不多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "8bc59383",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01-01</th>\n",
       "      <td>0.859055</td>\n",
       "      <td>-0.882752</td>\n",
       "      <td>-2.485008</td>\n",
       "      <td>-0.220455</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-02</th>\n",
       "      <td>0.535175</td>\n",
       "      <td>-0.312689</td>\n",
       "      <td>2.486631</td>\n",
       "      <td>0.333440</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-03</th>\n",
       "      <td>-0.555540</td>\n",
       "      <td>-2.020282</td>\n",
       "      <td>0.076171</td>\n",
       "      <td>-1.017064</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-04</th>\n",
       "      <td>1.174328</td>\n",
       "      <td>-1.167818</td>\n",
       "      <td>0.945664</td>\n",
       "      <td>-0.441212</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-05</th>\n",
       "      <td>-0.306017</td>\n",
       "      <td>1.654402</td>\n",
       "      <td>-0.105530</td>\n",
       "      <td>-0.907193</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-01-06</th>\n",
       "      <td>-0.626613</td>\n",
       "      <td>-0.625535</td>\n",
       "      <td>0.792208</td>\n",
       "      <td>-1.674696</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D  F\n",
       "2023-01-01  0.859055 -0.882752 -2.485008 -0.220455  1\n",
       "2023-01-02  0.535175 -0.312689  2.486631  0.333440  2\n",
       "2023-01-03 -0.555540 -2.020282  0.076171 -1.017064  3\n",
       "2023-01-04  1.174328 -1.167818  0.945664 -0.441212  4\n",
       "2023-01-05 -0.306017  1.654402 -0.105530 -0.907193  5\n",
       "2023-01-06 -0.626613 -0.625535  0.792208 -1.674696  6"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用索引自动对齐新增列的数据\n",
    "s1 = pd.Series([1,2,3,4,5,6],index=pd.date_range('20230101',periods=6))\n",
    "df['F']=s1\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3d0b132",
   "metadata": {},
   "source": [
    "# 8.运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "82b0c1ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p: 0      4.318706\n",
      "1     13.524404\n",
      "2     15.440007\n",
      "3     10.708804\n",
      "4     15.218453\n",
      "5     13.559415\n",
      "6     19.944247\n",
      "7     17.739295\n",
      "8     11.095516\n",
      "9     12.079562\n",
      "10    11.731693\n",
      "11    13.662369\n",
      "12     1.799703\n",
      "13    12.817734\n",
      "14    17.182410\n",
      "15    15.904222\n",
      "16    12.471310\n",
      "17    14.101411\n",
      "18    18.057299\n",
      "19    13.643232\n",
      "20    21.117927\n",
      "21    -0.799695\n",
      "dtype: float64\n",
      "[-1.3179389  13.71802769 16.43226713 19.8330634 ]\n"
     ]
    }
   ],
   "source": [
    "# 获取一个数列中的min，max和四分位值\n",
    "from numpy import percentile\n",
    "p = pd.Series(np.random.normal(14,6,22))\n",
    "print('p:',p)\n",
    "# 伪随机数生成器，参数可以随机，产生的随机数序列不同\n",
    "state=np.random.RandomState(12)\n",
    "p = pd.Series(state.normal(14,6,22))\n",
    "print(percentile(p, q=[0,25,50,75.100]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "9e557ea4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B   C   D\n",
      "0  12   5  20  14\n",
      "1   4   2  16   3\n",
      "2   5  54   7  17\n",
      "3  44   3   3   2\n",
      "4   1   2   8   6\n",
      "               A          B          C          D\n",
      "count   5.000000   5.000000   5.000000   5.000000\n",
      "mean   13.200000  13.200000  10.800000   8.400000\n",
      "std    17.683325  22.840753   6.978539   6.730527\n",
      "min     1.000000   2.000000   3.000000   2.000000\n",
      "25%     4.000000   2.000000   7.000000   3.000000\n",
      "50%     5.000000   3.000000   8.000000   6.000000\n",
      "75%    12.000000   5.000000  16.000000  14.000000\n",
      "max    44.000000  54.000000  20.000000  17.000000\n",
      "(5, 4)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "13.2"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求pandas DataFrame中一列的平均值\n",
    "df = pd.DataFrame({\"A\":[12, 4, 5, 44, 1],\n",
    "                   \"B\":[5, 2, 54, 3, 2], \n",
    "                   \"C\":[20, 16, 7, 3, 8],\n",
    "                   \"D\":[14, 3, 17, 2, 6]})\n",
    "print(df)\n",
    "print(df.describe())\n",
    "# 获取行数和列数\n",
    "print(df.shape)\n",
    "df['A'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ef95f88",
   "metadata": {},
   "source": [
    "# 9.分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "063a87fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>one</td>\n",
       "      <td>1.040061</td>\n",
       "      <td>-1.211994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>one</td>\n",
       "      <td>1.214009</td>\n",
       "      <td>-0.967443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>two</td>\n",
       "      <td>0.170723</td>\n",
       "      <td>-0.648321</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bar</td>\n",
       "      <td>three</td>\n",
       "      <td>-0.466034</td>\n",
       "      <td>2.015278</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>foo</td>\n",
       "      <td>two</td>\n",
       "      <td>0.236605</td>\n",
       "      <td>2.032284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>bar</td>\n",
       "      <td>two</td>\n",
       "      <td>-0.896423</td>\n",
       "      <td>-1.396176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>foo</td>\n",
       "      <td>one</td>\n",
       "      <td>-0.184453</td>\n",
       "      <td>-0.918695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>foo</td>\n",
       "      <td>three</td>\n",
       "      <td>2.411561</td>\n",
       "      <td>0.222374</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A      B         C         D\n",
       "0  foo    one  1.040061 -1.211994\n",
       "1  bar    one  1.214009 -0.967443\n",
       "2  foo    two  0.170723 -0.648321\n",
       "3  bar  three -0.466034  2.015278\n",
       "4  foo    two  0.236605  2.032284\n",
       "5  bar    two -0.896423 -1.396176\n",
       "6  foo    one -0.184453 -0.918695\n",
       "7  foo  three  2.411561  0.222374"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',\n",
    "                            'foo', 'bar', 'foo', 'foo'],\n",
    "                 'B': ['one', 'one', 'two', 'three',\n",
    "                         'two', 'two', 'one', 'three'],\n",
    "                 'C': np.random.randn(8),\n",
    "                 'D': np.random.randn(8)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "b689264e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A\n",
       "bar    3\n",
       "foo    5\n",
       "Name: B, dtype: int64"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算每组汇总数据\n",
    "df.groupby('A')['B'].count()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "b141c319",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    B    \n",
       "bar  one      1.214009\n",
       "     three   -0.466034\n",
       "     two     -0.896423\n",
       "foo  one      0.855608\n",
       "     three    2.411561\n",
       "     two      0.407327\n",
       "Name: C, dtype: float64"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多列分组后生成多层索引，用sum\n",
    "df.groupby(['A','B']).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "983c9939",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A      B\n",
       "one    A    2\n",
       "       B    2\n",
       "       C    2\n",
       "three  A    1\n",
       "       B    1\n",
       "       C    1\n",
       "two    A    1\n",
       "       B    1\n",
       "       C    1\n",
       "Name: D, dtype: int64"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['A','B'])['D'].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84210abd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f5ac8c65",
   "metadata": {},
   "source": [
    "# 10.数据透视表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "ba3fe17a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>one</td>\n",
       "      <td>A</td>\n",
       "      <td>foo</td>\n",
       "      <td>-0.708537</td>\n",
       "      <td>1.581515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>one</td>\n",
       "      <td>B</td>\n",
       "      <td>foo</td>\n",
       "      <td>0.416480</td>\n",
       "      <td>-0.797838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>two</td>\n",
       "      <td>C</td>\n",
       "      <td>foo</td>\n",
       "      <td>0.949180</td>\n",
       "      <td>0.179512</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>three</td>\n",
       "      <td>A</td>\n",
       "      <td>bar</td>\n",
       "      <td>2.471916</td>\n",
       "      <td>0.624506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>one</td>\n",
       "      <td>B</td>\n",
       "      <td>bar</td>\n",
       "      <td>1.051807</td>\n",
       "      <td>0.726736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>one</td>\n",
       "      <td>C</td>\n",
       "      <td>bar</td>\n",
       "      <td>-1.180544</td>\n",
       "      <td>-0.779099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>two</td>\n",
       "      <td>A</td>\n",
       "      <td>foo</td>\n",
       "      <td>-0.671896</td>\n",
       "      <td>1.143046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>three</td>\n",
       "      <td>B</td>\n",
       "      <td>foo</td>\n",
       "      <td>-0.710461</td>\n",
       "      <td>0.549787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>one</td>\n",
       "      <td>C</td>\n",
       "      <td>foo</td>\n",
       "      <td>-0.204416</td>\n",
       "      <td>0.636105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>one</td>\n",
       "      <td>A</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.556543</td>\n",
       "      <td>0.016572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>two</td>\n",
       "      <td>B</td>\n",
       "      <td>bar</td>\n",
       "      <td>-1.293918</td>\n",
       "      <td>0.617387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>three</td>\n",
       "      <td>C</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.135055</td>\n",
       "      <td>-0.029285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        A  B    C         D         E\n",
       "0     one  A  foo -0.708537  1.581515\n",
       "1     one  B  foo  0.416480 -0.797838\n",
       "2     two  C  foo  0.949180  0.179512\n",
       "3   three  A  bar  2.471916  0.624506\n",
       "4     one  B  bar  1.051807  0.726736\n",
       "5     one  C  bar -1.180544 -0.779099\n",
       "6     two  A  foo -0.671896  1.143046\n",
       "7   three  B  foo -0.710461  0.549787\n",
       "8     one  C  foo -0.204416  0.636105\n",
       "9     one  A  bar -0.556543  0.016572\n",
       "10    two  B  bar -1.293918  0.617387\n",
       "11  three  C  bar -0.135055 -0.029285"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 3,\n",
    "                    'B': ['A', 'B', 'C'] * 4,\n",
    "                    'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,\n",
    "                    'D': np.random.randn(12),\n",
    "                    'E': np.random.randn(12)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "22801c2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">D</th>\n",
       "      <th colspan=\"2\" halign=\"left\">E</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">one</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.556543</td>\n",
       "      <td>-0.708537</td>\n",
       "      <td>0.016572</td>\n",
       "      <td>1.581515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>1.051807</td>\n",
       "      <td>0.416480</td>\n",
       "      <td>0.726736</td>\n",
       "      <td>-0.797838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.180544</td>\n",
       "      <td>-0.204416</td>\n",
       "      <td>-0.779099</td>\n",
       "      <td>0.636105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">three</th>\n",
       "      <th>A</th>\n",
       "      <td>2.471916</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.624506</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.710461</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.549787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.135055</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.029285</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">two</th>\n",
       "      <th>A</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.671896</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.143046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-1.293918</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.617387</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.949180</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.179512</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                D                   E          \n",
       "C             bar       foo       bar       foo\n",
       "A     B                                        \n",
       "one   A -0.556543 -0.708537  0.016572  1.581515\n",
       "      B  1.051807  0.416480  0.726736 -0.797838\n",
       "      C -1.180544 -0.204416 -0.779099  0.636105\n",
       "three A  2.471916       NaN  0.624506       NaN\n",
       "      B       NaN -0.710461       NaN  0.549787\n",
       "      C -0.135055       NaN -0.029285       NaN\n",
       "two   A       NaN -0.671896       NaN  1.143046\n",
       "      B -1.293918       NaN  0.617387       NaN\n",
       "      C       NaN  0.949180       NaN  0.179512"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df, values=['D', 'E'], index=['A', 'B'], columns=['C'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "b68b27e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.empty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a2a0c5d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
